package com.linxz.ui.autorecycleview;

import android.content.Context;
import android.graphics.PointF;
import android.util.DisplayMetrics;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author Linxz
 * 创建日期：2019年06月04日 09:18
 * version：v4.0.1
 * 描述：
 */
public class ScrollSpeedLinearLayoutManger2 extends LinearLayoutManager {

    private float MILLISECONDS_PER_INCH = 0.03f;
    private Context contxt;

    public ScrollSpeedLinearLayoutManger2(Context context) {
        super(context);
        this.contxt = context;
    }

    @Override

    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    @Override
                    public PointF computeScrollVectorForPosition(int targetPosition) {
                        return ScrollSpeedLinearLayoutManger2.this.computeScrollVectorForPosition(targetPosition);
                    }

                    @Override
                    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                        setSpeedSlow();
                        return MILLISECONDS_PER_INCH / displayMetrics.density;
                        // return 700;
                       //返回滑动一个pixel需要多少毫秒
                    }
                };

        linearSmoothScroller.setTargetPosition(position);

        startSmoothScroll(linearSmoothScroller);

    }

    public void setSpeedSlow() {

//自己在这里用density去乘，希望不同分辨率设备上滑动速度相同

//0.3f是自己估摸的一个值，可以根据不同需求自己修改

        MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 3f;

    }

    public void setSpeedFast() {

        MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 0.03f;

    }


}
